#CROSS = mips-linux-uclibc-gnu-
CC    = $(CROSS)gcc
CPP   = $(CROSS)g++
AR    = $(CROSS)ar
STRIP = $(CROSS)strip

################# 产品型号选择 ##################
# PRODUCT_MODEL = B1
PRODUCT_MODEL = B5

ifeq ($(PRODUCT_MODEL), B1)
	TARGET_NAME = upgrade_generate_b1
	CFLAGS += -DB1_PRODUCT
else ifeq ($(PRODUCT_MODEL), B5)
	TARGET_NAME = upgrade_generate_b5
	CFLAGS += -DB5_PRODUCT
endif

# 生成的目标文件
# TARGET_NAME = upgrade_generate
ifeq ($(debug), 1)
    TARGET := $(TARGET_NAME)_debug
else
    TARGET := $(TARGET_NAME)
endif

# 保存编译产生临时文件目录
ifeq ($(debug), 1)
    OBJ_DIR = obj/debug
else
    OBJ_DIR = obj/release
endif

CFLAGS += -Wall -Werror -DCFG_DEBUG_LOG_ON
CFLAGS += -I include/

# 系统链接库
LFLAGS += -lpthread -lm -lrt -lz

# 指定要编译程序
#C_FILe = main.c test1.c src1/test2.c
# 指定当前目录所有程序
C_FILe = $(wildcard src/*.c)

OBJ_O = $(patsubst %.c, $(OBJ_DIR)/%.o, $(C_FILe))
OBJ_D = $(patsubst %.o, %.d, $(OBJ_O))

.PHONY: all print clean cleanall

all: $(TARGET)
	@echo target: $(TARGET)

$(TARGET): $(OBJ_O) $(LDLIBS)
	@echo here?
	$(CC) $(CFLAGS) -o $(TARGET) $^ $(LFLAGS)
ifneq ($(debug), 1)
	$(STRIP) $(TARGET)
endif

$(OBJ_DIR)/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJ_DIR)/%.d: %.c
	@mkdir -p $(dir $@)
	@rm -f $@
	$(CC) $(CFLAGS) -MM -MD $^ -o $@
	@sed 's/.*\.o/$(subst /,\/, $(dir $@))&/g' $@ >$@.tmp
	@mv $@.tmp $@

#判断是否为clean print,若是则不生成.d
ifeq ($(filter $(MAKECMDGOALS), clean print),)
sinclude $(OBJ_D)
endif

print:
	@echo -e "\033[33m"
	@echo "TARGET : $(TARGET)"
	@echo "OBJ_O  : $(OBJ_O)"
	@echo "OBJ_D  : $(OBJ_D)" 
	@echo -e "\033[0m"

clean:
	find $(OBJ_DIR)/ -type f -name 'core' -print | xargs rm -f
	find $(OBJ_DIR)/ -type f -name '*.o'  -print | xargs rm -f
	find $(OBJ_DIR)/ -type f -name '*.d'  -print | xargs rm -f
	rm -rf $(TARGET)

cleanall:
	rm -rf obj_release 
	rm -rf obj_debug
	rm -rf $(TARGET_NAME)
	rm -rf $(TARGET_NAME)_debug

